package cn.brilliantfuture.commentry.lockers.impl;

import java.util.List;

import javax.ejb.Stateless;
import javax.jws.WebService;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.apache.log4j.Logger;
import org.jboss.wsf.spi.annotation.WebContext;

import cn.brilliantfuture.commentry.entity.StorageGrid;
import cn.brilliantfuture.commentry.lockers.LockersMessageRemote;
import cn.brilliantfuture.commentry.lockers.LockersMessageWebService;

/**
 * 
 * Title: Communication Entry
 * 
 * Author:  LiTian  
 *
 * Date: 2012-5-15
 * 
 * Description:
 * 
 */
@Stateless
@WebService(endpointInterface = "cn.brilliantfuture.commentry.lockers.LockersMessageWebService")
@WebContext(contextRoot = "/commentry", urlPattern = "/LockersMessageWebService")
public class LockersMessageImpl implements LockersMessageWebService, LockersMessageRemote {

	protected static final Logger logger = Logger.getLogger(LockersMessageImpl.class);
	
	@PersistenceContext(unitName = "commentry-jpa")
	private EntityManager entityManager;
	
	@SuppressWarnings("unchecked")
	public String getLockersSn(String deviceSn) {
		logger.info("LockersMessageImpl.getLockersSn(String deviceSn) ...");
		List<Object> list = entityManager.createNativeQuery("select lockers_sn from lockers where device_sn = '" + deviceSn + "'").getResultList();
		
		if (null != list && list.size() > 0)
			return list.get(0).toString();
		
		return null;
	}
	
	@SuppressWarnings("unchecked")
	public Integer getLockersID(String deviceSn) {
		logger.info("LockersMessageImpl.getLockersID(String deviceSn) ...");
		List<Object> list = entityManager.createNativeQuery("select lockers_id from lockers where device_sn = '" + deviceSn + "'").getResultList();
		
		if (list.size() > 0)
			return (Integer)list.get(0);
		
		return null;
	}

	@SuppressWarnings("unchecked")
	public String getStorageGridMessage(String deviceSn, int gridNum){
		
		logger.info("getStorageGridMessage(String deviceSn, int gridNum) => deviceSn -> " + deviceSn + " gridNum -> " + gridNum);
		
		JSONObject result = new JSONObject();
		JSONArray infos = new JSONArray();
		
		List<StorageGrid> list = entityManager.createQuery("select sg from StorageGrid sg where sg.deleted = 0 and sg.lockers.device.deviceSn = '" + deviceSn + "' order by sg.gridNum asc").getResultList();
		
		if (null != list && list.size() > 0){
			result.accumulate("gridNum", list.size());

			for (StorageGrid storageGrid : list) {
				JSONObject info = new JSONObject();
				info.accumulate("gridType", storageGrid.getGridType());
				info.accumulate("businessType", storageGrid.getBusinessType());
				info.accumulate("gridSize", storageGrid.getGridSize());
				infos.add(info);
			}
		} else {
			for (int i = 0; i < gridNum; i++) {
				JSONObject info = new JSONObject();
				info.accumulate("gridType", 0);
				info.accumulate("businessType", 0);
				info.accumulate("gridSize", 0);
				infos.add(info);
			}
		}
		
		result.accumulate("storageGrids", infos);
		logger.info(result.toString());
		return result.toString();
	}
}
